<?php

namespace app\api\controller;
use app\api\dao\GoodsDao;
use app\api\dao\OrderDao;
use app\api\server\Zt;
use app\api\server\HelperZt;
use app\Request;

//背景调查
class Survey extends Api
{
    public $payUrl="https://center.rjxx.com.cn/api/paycommon";

    //产品列表
    public function product(Request $request)
    {
        $goodsList=(new goodsDao())->list($request);
        $this->success($goodsList);
    }

    //创建订单
    public function order(Request $request)
    {
        //参数数据验证
        if(empty($this->productInfo)) $this->error("商品信息错误！");
        $require=json_decode($this->productInfo["require"],1);
        if(empty($require)) $this->error("商品信息非法！");
        $pay_type=$request->param("pay_type",10);
        //业务参数校验
        foreach ($require as $row)
        {
            if($request->param($row["field"]))     $param[$row["field"]]=$request->param($row["field"]);
            if($row["required"])
            {
                if(empty($param[$row["field"]])) $this->error("请填写".$row["name"]);
            }
        }

        //订单创建入库
        $orderInfo=(new OrderDao())->add($this->productInfo,$this->userInfo,$param,$this->appid,$pay_type);

        //参数支付调用
        $headers = array("token: ".$this->token,"appid: ".$this->appid,"sourceFrom: xcx");
        $payData = [
            "order_no"=>$orderInfo["order_no"],
            "money"=>$orderInfo["price"],
            "order_name"=>$orderInfo["order_name"],
            "paytype"=>$orderInfo["pay_type"],
            "notify_url"=>$domain= "http://" .$request->server("HTTP_HOST")."/api/survey/notify",//支付通知地址
        ];
        $paySign=$this->postRequest($this->payUrl,$payData,$headers);

        (new OrderDao())->update($orderInfo["order_no"],["pay_sign"=>json_encode($paySign,320)]);
        //返回订单支付
        $this->success([
            "orderInfo"=>$orderInfo,
            "paySign"=>$paySign["data"],
        ]);
    }

    //订单列表
    public function orderList(Request $request)
    {
        $param=$request->param();
        $orderList=(new OrderDao())->list($this->userInfo["user_id"],$this->appid,$param);
        $this->success($orderList);
    }

    //订单详情
    public function orderDetail(Request $request)
    {
        $order_no=$request->param("order_no");
        if(empty($order_no)) $this->error("订单号不存在！");
        $orderDetail=(new OrderDao())->detail($order_no,$this->userInfo["user_id"],$this->appid);
        $this->success($orderDetail);
    }

    //报告详情
    public function reportDetail(Request $request)
    {
        $order_no=$request->param("order_no");
        if(empty($order_no)) $this->error("订单号不存在！");
        $reportDetail=(new OrderDao())->report($order_no);
        $this->success($reportDetail);
    }

    //网络post请求
    public function postRequest($url,$data=[],$headers=[])
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers );
        $output = curl_exec($curl);
        curl_close($curl);
        $output=json_decode($output,1);
        return $output;
    }

    //支付成功回调
    public function notify(Request $request)
    {
        $order_no=$request->param("order_no");
        $orderInfo=(new OrderDao())->find($order_no);
        if($orderInfo && $orderInfo["pay_status" == 10])
        {
            //完成支付状态
            (new OrderDao())->finishPay($order_no);
            //创建第三方订单(默认成都)
            $thirdOrderInfo=(new Zt())->initiate($orderInfo);

            \think\facade\Queue::push(\app\job\ZtOrder::class, $thirdOrderInfo,"ZtOrder");
        }
        $this->success("success");
    }

    //第三方创建订单查询
    public function ztOrder(Request $request)
    {
        $order_no=$request->param("order_no");
        $orderInfo=(new OrderDao())->find($order_no);
        //创建第三方订单(默认成都)
//        $thirdOrderInfo=(new Zt())->initiate($orderInfo);


        //订单队列测试
        $thirdOrderInfo["order_no"]="SV2024110410173786531581";
        $thirdOrderInfo["third_order_no"]="211492307012583424";
        \think\facade\Queue::push(\app\job\ZtOrder::class, $thirdOrderInfo,"ZtOrder");


        //$data=(new Zt())->report($thirdOrderInfo["third_order_no"]);


    }







}